package com.jiazhong.mingxing.boot.boot04.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Set;

@SpringBootTest
@Slf4j
public class SetApp {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        SetOperations<String, String> forSet = stringRedisTemplate.opsForSet();
        forSet.add("A", "aa");
        forSet.add("A", "bb", "cc", "dd", "aa", "11", "22");
    }

    @Test
    public void b() {
        SetOperations<String, String> forSet = stringRedisTemplate.opsForSet();
        // 获取所有数据
        Set<String> members = forSet.members("A");
        log.info("members:{}", members);
        // 获取随机的一个元素
        String randomMember = forSet.randomMember("A");
        log.info("randomMember:{}", randomMember);
        List<String> randomMembers = forSet.randomMembers("A", 3);
        Set<String> distinctRandomMembers = forSet.distinctRandomMembers("A", 3);
        log.info("randomMembers:{}", randomMembers);
        log.info("distinctRandomMembers:{}", distinctRandomMembers);
    }

    @Test
    public void c() {
        SetOperations<String, String> forSet = stringRedisTemplate.opsForSet();
        Long size = forSet.size("A");
        log.info("size:{}", size);
        Boolean member = forSet.isMember("A", "aa");
        log.info("member:{}", member);
        // 获取并删除
        String pop = forSet.pop("A");
        log.info("pop:{}", pop);
    }

    @Test
    public void d() {
        SetOperations<String, String> forSet = stringRedisTemplate.opsForSet();
        forSet.add("B", "aa", "bb", "cc", "dd");
        forSet.add("C", "aa", "mm", "kk", "cc", "11");
        Set<String> union = forSet.union("B", "C");
        log.info("union:{}", union);
        Set<String> difference = forSet.difference("B", "C");
        log.info("difference:{}", difference);
        Set<String> intersect = forSet.intersect("B", "C");
        log.info("intersect:{}", intersect);
    }
}
